// pages/shop_details/index.js
var app = getApp();
var WxParse = require('../../components/wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://www.jlzfnm.com/UploadFiles/image/20140807/20140807064701_70688.jpg',
    ],
    duration: 500,
    spece_choose: 0,
    activeIndex: 0,
    // sliderLeft: 0,
    sliderOffset: 0,
    spece_list: ["1*袋(5斤)", "1*袋(5斤)", "1*袋(5斤)"],
    nav_list: ["详情", "规格"],
    head_opacity: 0,
    shop_detail: {},
    cart_num: app.globalData.cart_num,
    allprice: 0,
    scroll_top: 0,
    current: 1,
    balls: [
      { isShow: false, animationData: null },
      { isShow: false, animationData: null }
    ],
    
  },
  onShareAppMessage: function () {
    return {
      title: '速冻在线',
      desc: '速冻在线商城，西南最大冻品供应商',
      path: '/page/shop_details/shop_details'
    }
  },
  spece_active: function (e) {
    this.setData({
      spece_choose: e.currentTarget.id
    })
  },
  tabClick: function (e) {
    var that = this
    var id = e.currentTarget.id
    console.log(e)
    wx.createSelectorQuery().select('.container').scrollOffset(function (res) {
      var pagescrollTop = res.scrollTop
      wx.createSelectorQuery().select('#title_' + id).boundingClientRect(function (rect) {
        console.log(rect)
        that.setData({
          scroll_top: rect.top + pagescrollTop - 80
        })
      }).exec()
    }).exec()
    this.setData({
      head_opacity: 1,
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  // topArr:[],
  scroll: function (e) {
    var that = this
    var nav_list = this.data.nav_list
    if (e.detail.scrollTop <= 500) {
      this.setData({
        head_opacity: e.detail.scrollTop / 200
      })
    }
    for (let i in nav_list) {
      wx.createSelectorQuery().select('#title_' + i).boundingClientRect(function (res) {
        if (res.top >= 90 && res.top <= 200) {
          wx.createSelectorQuery().select('.nav_' + i).boundingClientRect(function (rect) {
            that.setData({
              sliderOffset: rect.left,
              activeIndex: i
            })
          }).exec()
        }
      }).exec()
    }
  },
  add_cart: function (e) {
    var that = this
    app.ball_animate(e,this)
    app.add_cart(e, this, function () {
      app.all_price(that)
    })
  },
  s_change: function (e) {
    this.setData({
      current: e.detail.current + 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    var sliderWidth = 30;
    var nav_list = this.data.nav_list
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderOffset: res.windowWidth / that.data.nav_list.length * that.data.activeIndex
        });
      }
    });
    app.ajax({
      url: app.host + 'mall/goods/detail',
      data: { goods_id: options.goods_id },
      success: function (res) {
        var description = res.data.data.description
        that.setData({
          shop_detail: res.data.data,
        })
        wx.hideLoading()
        WxParse.wxParse('description', 'html', description, that)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.cart_num(this)
    app.all_price(this)
    // wx.createSelectorQuery().select('page').scrollOffset(function (res) {
    //   console.log(res)
    // }).exec()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  add_freq: function (e) {
    app.ajax({
      url: app.host + 'mall/Freqlist/add_freq',
      data: {
        goods_id: e.currentTarget.dataset.goods_id
      },
      success: function (res) {
        console.log(res);
          wx.showToast({
            title: res.data.msg,
          })
      },
    });
  },
})